
<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type === 1" type="text" @click="handleAdd(2,row.id)">添加</el-button>
              <el-button type="text" @click="handleEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="handleRemove(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <!-- 弹框 -->
      <el-dialog v-if="showDialog" :visible.sync="showDialog" :title="title" @close="close">
        <!-- 表单内容 -->
        <el-form ref="formDataRules" label-width="100px" :model="formData" :rules="formDataRules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="handleSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, updatePermission, getPermissionDetail, addPermission, delPermission } from '@/api/permission'
import { handleTreeArray } from '@/utils'

export default {
  name: 'Permission',
  data() {
    // 添加时的校验： 名字不能取子元素的名字 不能与兄弟重复
    const validatorName = (rule, val, callback) => {
      // console.log(rule, val)
      // this.formData.pid === this.
      // console.log(this.list.filter(t => t.id === this.formData.pid))
      // const arr = this.list.filter(t => t.id === this.formData.pid)
      const obj = this.list.find(t => t.id === this.formData.pid)

      const arr = obj ? obj.children : this.list.filter(t => t.pid === this.formData.pid)

      arr.find(t => t.name === val) && callback(new Error(val + '已经存在'))

      // console.log(obj)
      // if (obj) {
      //   // 二级
      //   // console.log(obj)

      // } else {
      //   // 一级
      //   // console.log(this.list.filter(t => t.pid === this.formData.pid))
      //   .find(t => t.name === val) && callback(new Error(val + '已经存在'))
      // }
      // if (this.formData.pid === '0') {
      // console.log(1111111)
      // console.log(this.list.find(t => t.pid === '0' && t.name === val))
      // this.list.find(t => t.pid === '0' && t.name === val) && callback(new Error(val + '已经存在'))
      // } else {
      callback()
      // }
    }

    return {
      formDataRules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          { validator: validatorName, trigger: 'blur' }
        ]

      },
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      isEdit: false
    }
  },
  computed: {
    title() {
      return this.isEdit ? '编辑权限' : '添加权限'
    }
  },
  created() {
    this.loadPermissionList()
  }, methods: {
    // 弹框关闭
    close() {
      console.log(11111)
      this.formData = {}
    },
    // 添加权限
    handleAdd(t, k) {
      console.log(k)
      this.formData = {}
      this.showDialog = true
      this.formData.type = t
      this.formData.pid = k
      this.isEdit = false
    },
    async loadPermissionList() {
      const { data } = await getPermissionList()
      // console.log('从后端获取的数据的是', res)

      this.list = handleTreeArray(data)
    },
    // 编辑
    async handleEdit(id) {
      const { data } = await getPermissionDetail(id)
      this.formData = data
      this.isEdit = true
      this.showDialog = true
    },

    // 添加/编辑提交
    async handleSubmit() {
      try {
        await (this.isEdit ? updatePermission(this.formData) : addPermission(this.formData))
        this.showDialog = false
        this.loadPermissionList()
        this.$message.success(this.isEdit ? '编辑成功' : '添加成功')
        this.formData = {}
      } catch (error) {
        console.log(error)
      }
    },
    // 删除
    async handleRemove(id) {
      try {
        await this.$confirm('你确定要删除吗？').then(() => {
          this.doDel(id)
        })
      } catch (error) {
        this.$message.info('取消操作了捏')
      }
    },
    async  doDel(id) {
      try {
        const res = await delPermission(id)
        console.log(res)

        this.loadPermissionList()
        this.$message.success('删除成功')
      } catch (err) {
        console.log(err)
        this.$message.error('删除失败')
      }
    }
  }
}
</script>
